
package com.hd.determine.modules.alarm.determine.judges;

import com.alibaba.fastjson.JSON;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.GenUtil;
import com.hd.common.utils.SpringContextUtils;
import com.hd.common.utils.ValueUtils;
import com.hd.determine.config.redis.prefix.CacheKey;
import com.hd.determine.config.redis.utils.JedisUtil;
import com.hd.determine.core.utils.StaticPropUtil;
import com.hd.determine.modules.alarm.Determiner;
import com.hd.determine.modules.alarm.model.AlarmRule;
import com.hd.determine.modules.vehicle.model.VehicleEndPos;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title OffGpsDeterminer.java
 * @Description 疲劳驾驶研判者
 * @Package com.hd.sup.modules.alarm.determine.judges
 * @date 2021/1/29 11:26 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
public class FatigueDeterminer extends Determiner<VehicleEndPos> {


    private final JedisUtil jedisUtil = SpringContextUtils.getBean(JedisUtil.class);

    public FatigueDeterminer(VehicleEndPos data, AlarmRule alarmRule) {
        super(data, alarmRule);
    }


    @Override
    public void startDetermine() {

        String platNo = data.getPlatNo();
        String platNoColor = data.getPlatNoColor();
        Date continuousDrivingStartTime = data.getContinuousDrivingStartTime();
        Date updateTime = data.getUpdateTime();
        Date gpsTime = data.getGpsTime();
        Date now = new Date();

        //研判因子
        if (continuousDrivingStartTime == null || updateTime == null || gpsTime == null) {
            return;
        }

        if(StaticPropUtil.SHOW_ALARM_LOG){
            log.info("[alarmDeterminer] 即将开始研判 车牌:{} 颜色: {}  疲劳驾驶报警,研判因子:\n continuousDrivingStartTime:{} \n  updateTime:{} \n gpsTime:{} \n",
                    platNo,
                    platNoColor,
                    continuousDrivingStartTime,
                    updateTime,
                    gpsTime
            );
        }


        long minutes = ValueUtils.parseLong(DateUtil.getDiffMins(gpsTime, continuousDrivingStartTime));
        if (minutes > alarmRule.getContinuousMinute()) {
            //大于配置的连续驾驶最大时长 判定为疲劳驾驶
            data.setBizAlarmType(BizConstant.AlarmType.FATIGUE_DRIVE.key());
            needProduce = true;
            return;
        }


        //更新停车时间
        Date parking_time = data.getParkingTime();
        if (parking_time != null) {
            long parkingDuration = ValueUtils.parseLong(DateUtil.getDiffMins(now, parking_time));
            if (parkingDuration > alarmRule.getRestMinute()) {
                //停车时间超过配置的时间 判定为停车
                String cacheKey = GenUtil.cacheKey(CacheKey.END_POSITION_KEY, platNo, BizConstant.Common.UNDERLINE.key(), platNoColor);
                data.setContinuousDrivingStartTime(now);
                data.setDrivingDuration(BizConstant.Common.DURATION_INIT_VAL.key());
                //刷新缓存
                jedisUtil.set(cacheKey, JSON.toJSONString(data), 0);
                if(StaticPropUtil.SHOW_ALARM_LOG) {
                    log.info("[rabbitmq] 监测到车辆:{} {} 已停车 , 连续驾驶时间已刷新", platNo, platNoColor);
                }
            }
        } else {
            long dataDuration = ValueUtils.parseLong(DateUtil.getDiffMins(now, updateTime));
            if (dataDuration > alarmRule.getRestMinute()) {
                //停车时间超过配置的时间 判定为停车
                String cacheKey = GenUtil.cacheKey(CacheKey.END_POSITION_KEY, platNo, BizConstant.Common.UNDERLINE.key(), platNoColor);
                data.setContinuousDrivingStartTime(now);
                data.setDrivingDuration(BizConstant.Common.DURATION_INIT_VAL.key());
                //刷新缓存
                jedisUtil.set(cacheKey, JSON.toJSONString(data), 0);
                if(StaticPropUtil.SHOW_ALARM_LOG) {
                    log.info("[rabbitmq] 监测到车辆:{} {} 无数据 , 连续驾驶时间已刷新", platNo, platNoColor);
                }
            }
        }

        //停止报警
        needStop = true;
    }
}
